import { createStore } from "vuex";
import { saveLocal, getLocal } from "../utils/storage";
import {getToken,setToken,getRefreshToken,setRefreshToken,getObjToken} from "../utils/token"
import {addRoutes} from "../router"
import {refreshToken as refreshTokenApi,getRoutes,getMenus} from "@/api/base"
export default createStore({
  state: {
    tenantId: "",
    token: "",
    isAdmin: false,
    userId: "",
    refreshToken: "",
    userInfo: {},
    menus: [], // 需要渲染的菜单数据
    routes:[],
    authorityList: [],
    tentTitle: "",
    loginStatus: false, // 登录状态
    breadName: "", // 面包屑名称
    // fold:true,
  },
  getters: {
    tenantId: (state) => state.tenantId ?? getLocal("tenantId"),
    token: (state) => state.token ?? getToken(),
    isAdmin: (state) => state.isAdmin ?? getLocal("isAdmin"),
    userId: (state) => state.userId ?? getLocal("userId"),
    refreshToken: (state) => state.refreshToken ?? getRefreshToken(),
    userInfo: (state) => state.userInfo ?? getLocal("userInfo"),
    menus: (state) => state.menus ?? getLocal("menus"),
    routes: (state) => state.routes ?? getLocal("routes"),
    loginStatus: (state) => state.loginStatus,
  },
  mutations: {
    // setFold(state, val: boolean) {
    //   state.fold = val;
    // },
    setBreadName(state, val: string) {
      state.breadName = val;
    },
    setTenantId(state, val: string) {
      state.tenantId = val;
      saveLocal("tenantId", val);
    },
    setToken(state, val: string) {
      state.token = val;
      setToken(val)
    },
    setRoutes(state, payload) {
      state.routes = payload;
    },
    setLoginStatus(state, val: boolean) {
      state.loginStatus = val;
    },
    setAuthorityList(state, payload) {
      state.authorityList = payload;
    },
    setIsAdmin(state, val: boolean) {
      state.isAdmin = val;
      saveLocal("isAdmin", val);
    },
    setUserId(state, val: string) {
      state.userId = val;
      saveLocal("userId", val);
    },
    setRefreshToken(state, val: string) {
      state.refreshToken = val;
      setRefreshToken(val)
    },
    setUserInfo(state, val: any) {
      state.userInfo = val;
      saveLocal("userInfo", val);
    },
    setMenus(state, val) {
      state.menus = val;
      saveLocal("menus", val);
    },
    setTentName(state, val) {
      state.tentTitle = val;
      saveLocal("tentName", val);
    },
  },
  actions: {
    login({ commit }, data) {
    },
    // 免登录
    noLogin({commit,getters,dispatch},obj:string|any){
      const d = typeof obj === 'string' ? {token:obj} : getObjToken(obj)
      if(d.token){
        commit("setToken", d.token);
        commit("setLoginStatus", true);
      }
      if(d.refreshToken){
        commit("setRefreshToken",d.refreshToken)
      }
    },
    // 登出
    logout({ commit }) {
      commit("setToken", "");
      commit("setUserInfo", {});
      commit("setMenus", []);
      commit("setLoginStatus", false);
      commit("setIsAdmin", false);
      // clearLocal();
    },
    // 动态路由
    refreshRouter({getters}){
      const routes = getters.routes;
      if(!Array.isArray(routes) || routes.length === 0){return ;}
      addRoutes(routes)
    },
    // 刷新 token
    async resetToken({getters,commit}) {
      const rt:string = getters.refreshToken;
      if(rt){
        const {data = {}} = await refreshTokenApi(rt)
        //
        const {token,refreshToken} = data;
        if(token && refreshToken){
          commit("setToken",token)
          commit("setRefreshToken",refreshToken)
        }
      }
    },
    // 重置路由数据
    async resetRoutes({commit,dispatch}){
      const {data = {}} = await getRoutes();
      //
      const routes = data?.data || [] // 扁平 list ，不带 children,如果存在父子关系，则以字段 parentName 确定
      //
      commit("setRoutes",routes);
      //
      dispatch("refreshRouter")
    }
  },
  modules: {},
});
